package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.inn.UserInnInfo;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserCookDaoImpl extends TempDataObjectDao<UserInnInfo> {
    @Override
    protected boolean addData(UserInnInfo data) {

        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_inn_cook(userId,dishesId,patronsId,dishesNum,cookStartTime,lastClickMoneyTime,lastValue,totalCookTimes,hotRate) " +
                "VALUES(?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getDishesId());
            call.setInt(i++, data.getPatronsId());
            call.setInt(i++,data.getDishesNum());
            call.setLong(i++, data.getCookStartTime());
            call.setLong(i++, data.getLastClickMoneyTime());
            call.setLong(i++, data.getLastValue());
            call.setLong(i++,data.getTotalCookTimes());
            call.setInt(i++,data.getHotRate());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserInnInfo data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_inn_cook SET dishesId=?,patronsId=?,dishesNum=?,cookStartTime=?,lastClickMoneyTime=?,lastValue=?,totalCookTimes=?,hotRate=? WHERE userId=? ";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getDishesId());
            call.setInt(i++, data.getPatronsId());
            call.setInt(i++, data.getDishesNum());
            call.setLong(i++, data.getCookStartTime());
            call.setLong(i++, data.getLastClickMoneyTime());
            call.setLong(i++, data.getLastValue());
            call.setLong(i++,data.getTotalCookTimes());
            call.setInt(i++,data.getHotRate());
            call.setLong(i++,data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserInnInfo parseData(ResultSet rs) throws SQLException {

        UserInnInfo userInnInfo = new UserInnInfo();
        userInnInfo.setUserId(rs.getLong("userId"));
        userInnInfo.setDishesId(rs.getInt("dishesId"));
        userInnInfo.setPatronsId(rs.getInt("patronsId"));
        userInnInfo.setDishesNum(rs.getInt("dishesNum"));
        userInnInfo.setCookStartTime(rs.getLong("cookStartTime"));
        userInnInfo.setLastClickMoneyTime(rs.getLong("lastClickMoneyTime"));
        userInnInfo.setLastValue(rs.getLong("lastValue"));
        userInnInfo.setTotalCookTimes(rs.getLong("totalCookTimes"));
        userInnInfo.setHotRate(rs.getInt("hotRate"));
        return userInnInfo;
    }

    public UserInnInfo getUserCookInfo(long userId) {

        UserInnInfo userInnInfo = null;
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_inn_cook where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                userInnInfo = parseData(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return userInnInfo;
    }



}
